package com.zhongwang.mms.module.buy.service;

import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.gen.entity.BShoppingPlan;
import com.zhongwang.mms.gen.entity.User;
import com.zhongwang.mms.gen.mapper.BShoppingPlanChangeMapper;
import com.zhongwang.mms.gen.mapper.BShoppingPlanMapper;
import com.zhongwang.mms.module.buy.dao.BuyPlanDao;
import com.zhongwang.mms.module.buy.listner.ImportShoppingPlanRowNoListener;
import com.zhongwang.mms.module.buy.model.ImportShoppingPlanRowNoModel;
import com.zhongwang.mms.module.buy.model.ShoppingPlanModel;
import com.zhongwang.mms.module.half.model.HCheckDetailListener;
import com.zhongwang.mms.module.purchase.model.check.PCheckDetailExcelModel;
import com.zhongwang.mms.module.purchase.model.check.PCheckPlanModel;
import com.zhongwang.mms.module.system.log.LogService;
import com.zhongwang.mms.util.FileUtils;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static com.zhongwang.mms.util.BeanUtil.copy;

/**
 * @Description: 采购管理 -> 采购计划进度编辑 Service
 *
 * @Author: China.sgl
 * @Date: 2020/4/7 16:25
 */
@SuppressWarnings("ALL")
@Log4j2
@Service
public class EditBuyProgressService {

    @Autowired
    private LogService logService;
    @Autowired
    private BuyPlanDao buyPlanDao;
    @Autowired
    private BShoppingPlanMapper bShoppingPlanMapper;
    @Autowired
    private BShoppingPlanChangeMapper bShoppingPlanChangeMapper;

    @Value("${user.upload.path}")
    private String uploadPath;
    /**
     * 按条件查询已导出的采购计划
     *
     * @param shoppingPlanModel 条件
     * @return list
     */
    public List<ShoppingPlanModel> findBuyPlanByQuery(ShoppingPlanModel shoppingPlanModel) {
        if (shoppingPlanModel.getSppProjectCodeArrayStr() != null && !shoppingPlanModel.getSppProjectCodeArrayStr().equals("")) {
            String[] strArray = shoppingPlanModel.getSppProjectCodeArrayStr().split(",");
            shoppingPlanModel.setSppProjectCodeArray(Arrays.asList(strArray));
        }
        return buyPlanDao.findBuyPlanByProgressEdit(shoppingPlanModel);
    }

    /**
     * 编辑采购进度
     *
     * @param shoppingPlanModel 进度
     * @return 行数
     */
    @Transactional(rollbackFor = Exception.class)
    public int editPlanProgress(ShoppingPlanModel shoppingPlanModel) {
        BShoppingPlan plan = bShoppingPlanMapper.selectByPrimaryKey(shoppingPlanModel.getSppId());
        copy(shoppingPlanModel, plan);
        bShoppingPlanMapper.updateByPrimaryKey(plan);
        buyPlanDao.updateBuyPlanProgress(plan);
        logService.saveLog("采购计划进度编辑", "物料描述：" + plan.getSppMatDesc());
        return 1;
    }
    /**
     * 导出采购进度
     *
     * @param ShoppingPlanModel param
     * @return json
     */
    public void export(HttpServletResponse response, ShoppingPlanModel param) {
        List<ImportShoppingPlanRowNoModel> list = buyPlanDao.findImportShoppingPlanRowNoModelByWhere(param);
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        logService.saveLog("采购进度编辑——导出采购进度编辑列表","用户:【"+user.getName()+"】执行导出采购进度编辑列表操作！");
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.export(path, "采购进度编辑.xls", ImportShoppingPlanRowNoModel.class, "sheet1", list, response);
    }
    /**
     * 导入采购进度修改行号
     *
     * @param ShoppingPlanModel param
     * @return json
     */
    public JsonResult importExcel(MultipartFile file) {
        JsonResult result = new JsonResult();
        try {
            FileUtils.importExcel(file, ImportShoppingPlanRowNoModel.class, new ImportShoppingPlanRowNoListener(buyPlanDao, new ImportCallBack() {
                @Override
                public void onSuccess() {
                    result.setType(JsonResult.ResultType.SUCCESS);
                    result.setMsg("批量导入成功！");
                }

                @Override
                public void onWarning(List list) {
                    result.setType(JsonResult.ResultType.ERROR);
                    result.setMsg("批量导入失败！");
                    result.setData(list);
                }
            },logService));
        } catch (IOException e) {
           e.printStackTrace();
           log.error(e.getMessage());
            result.setType(JsonResult.ResultType.ERROR);
            result.setMsg("批量导入失败！");
        }
        return result;
    }
}
